package com.study.portal.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.study.portal.util.bean.LoginUesrBean;

public class StrutsUtil {
	public static final String STRUTS_ACTION_EXTENSION;
	static {
		Properties properties = readProperties("/struts.properties");
		STRUTS_ACTION_EXTENSION = properties
				.getProperty("struts.action.extension");
		System.out.println(STRUTS_ACTION_EXTENSION);
	}

	private static Properties readProperties(String propertiesPath) {
		Properties properties = new Properties();
		try {
			InputStream in = StrutsUtil.class
					.getResourceAsStream(propertiesPath);
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return properties;

	}

	public static boolean isGetMethod() {
		if ("GET".equals(ServletActionContext.getRequest().getMethod())) {
			return true;
		}
		return false;
	}

	/**
	 * 获取项目跟路径 <br/>
	 * Action 中调用
	 * 
	 * @return String:项目跟路径
	 */
	public static String getBasePath() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + path + "/";
		return basePath;
	}

	public static String getRequestUrl() {
		HttpServletRequest request = ServletActionContext.getRequest();
		return request.getRequestURL().toString();
	}

	public static String getRequestURI() {
		HttpServletRequest request = ServletActionContext.getRequest();
		return request.getRequestURI();
	}

	public static String getActionUrl(String actionName) {
		return getBasePath() + actionName + "." + STRUTS_ACTION_EXTENSION;
	}

	public static String getActionUrl(String actionName, String spaceName) {
		if (StringUtils.isEmpty(spaceName)) {
			return getActionUrl(actionName);
		}
		return getBasePath() + spaceName + "/" + actionName + "."
				+ STRUTS_ACTION_EXTENSION;
	}
	
	public static String getActionUrl(String actionName, Map<String, String> params) {
		String url = getActionUrl(actionName) + "?";

		Set<String> keys = params.keySet();
		for (Iterator<String> it = keys.iterator(); it.hasNext();) {
			String key = it.next();
			String val = params.get(key);
			try {
				url += key + "=" + URLEncoder.encode(val, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			if (it.hasNext()) {
				url += "&";
			}
		}
		return url;
	}

	public static String getActionUrl(String actionName, String spaceName,
			Map<String, String> params) {
		String url = "";
		if (StringUtils.isEmpty(spaceName)) {
			url = getActionUrl(actionName) + "?";
		} else {
			url = getBasePath() + spaceName + "/" + actionName + "."
					+ STRUTS_ACTION_EXTENSION + "?";
		}

		Set<String> keys = params.keySet();
		for (Iterator<String> it = keys.iterator(); it.hasNext();) {
			String key = it.next();
			String val = params.get(key);
			try {
				url += key + "=" + URLEncoder.encode(val, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			if (it.hasNext()) {
				url += "&";
			}
		}
		return url;
	}
	
	public static LoginUesrBean getLoginUserBean(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		return (LoginUesrBean)session.getAttribute(ConstantUtil.LOGIN_USER_BEAN);
	}
	
	/**
	 * 输出返回流
	 * @param str
	 * @throws IOException
	 */
	public static void write(String str) throws IOException{
	    ServletActionContext.getResponse().setContentType("text/html,charset=utf-8");
	    ServletActionContext.getResponse().getWriter().write(str);
	}

	public static void main(String[] args) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("name", "erer");
		params.put("name2", "erer");
		params.put("name3", "erer");
		params.put("name4", "哈哈的房");
		System.out.println(getActionUrl("action", "spaceName", params));
	}

}
